package com.horsent.scmdemo;

import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;
import realarm.hardwaretest.jniinterface.SerialPortManager;

/**
 * Date    2019/7/16
 * Author  WestWang
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText mAddEt;
    private EditText mDeleteEt;
    private TextView tv_show;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_layout);

        findViewById(R.id.mainDog).setOnClickListener(this);
        findViewById(R.id.mainCloseDog).setOnClickListener(this);
        findViewById(R.id.mainAsync).setOnClickListener(this);
        findViewById(R.id.mainAddBtn).setOnClickListener(this);
        findViewById(R.id.mainDeleteBtn).setOnClickListener(this);
        findViewById(R.id.mainQuery).setOnClickListener(this);
        mAddEt = findViewById(R.id.mainAddEt);
        mDeleteEt = findViewById(R.id.mainDeleteEt);
        tv_show = findViewById(R.id.tv_show);

        requestPermissions();
        //初始化刷卡监听
        initSerialPort();
        SerialPortManager.getInstance().showInfoView=new SerialPortManager.ShowInfoView() {
            @Override
            public void showInfo(String info) {
                tv_show.setText(info);
                Toast.makeText(MainActivity.this,info,Toast.LENGTH_SHORT).show();
            }
        };
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mainDog:
                // 看门狗握手
                dog();
                break;
            case R.id.mainCloseDog:
                // 同步更新
                closeDog();
                break;
            case R.id.mainAsync:
                // 同步更新
                async();
                break;
            case R.id.mainAddBtn:
                // 增加一条
                add();
                break;
            case R.id.mainDeleteBtn:
                // 删除一条
                delete();
                break;
            case R.id.mainQuery:
                // 查询数量
                query();
                break;

        }
    }

    private void dog() {
        SerialPortManager.getInstance().setCmdType(SerialPortManager.CMD_TYPE_WATCH_DOG);
        SerialPortManager.getInstance().watchDogHandshake();
    }
    private void closeDog() {
        SerialPortManager.getInstance().setCmdType(SerialPortManager.CMD_TYPE_WATCH_DOG);
        SerialPortManager.getInstance().closeDogHandshake();
    }

    private void async() {

        String[] data = new String[]{"0001A9C57809", "0002A9C57809", "0003A9C57809", "0004A9C57809",
                "0005A9C57809", "0006A9C57809", "0007A9C57809", "0008A9C57809", "0009A9C57809",
                "000AA9C57809"};

        SerialPortManager.getInstance().setCmdType(SerialPortManager.CMD_TYPE_ASYNC_HANDSHAKE);
        SerialPortManager.getInstance().updateData(data);
    }

    private void add() {
        String data = mAddEt.getText().toString().trim().toUpperCase();
        SerialPortManager.getInstance().setCmdType(SerialPortManager.CMD_TYPE_ADD_DATA);
        SerialPortManager.getInstance().addData(data);
    }

    private void delete() {
        String data = mDeleteEt.getText().toString().trim().toUpperCase();
        SerialPortManager.getInstance().setCmdType(SerialPortManager.CMD_TYPE_DELETE_DATA);
        SerialPortManager.getInstance().deleteData(data);
    }

    private void query() {
        SerialPortManager.getInstance().setCmdType(SerialPortManager.CMD_TYPE_QUERY_DATA);
        SerialPortManager.getInstance().queryNumber();
    }

    @SuppressLint("CheckResult")
    private void requestPermissions() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean it) throws Exception {
                        if (it) {
                            Log.e("MainActivity", "授权成功");
                            SerialPortManager.getInstance();
                        } else {
                            Log.e("MainActivity", "授权失败");
                        }
                    }
                });
    }


    /**
     * 串口初始化
     */
    private void initSerialPort() {
       /* SerialPortCardManager.getInstance().setSerialPortListener(new SerialPortListener() {
            @Override
            public void onReadResult(String result) {
                Log.d("card", "onReadResult: "+result);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });*/
   //     SerialPortCardManager.getInstance().init();
    }
}
